﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RobozzleModel;
using System.Text;

public partial class index_compat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string LoadPuzzle()
    {
        int puzzleId;
        if (!int.TryParse(Request.Params["puzzle"], out puzzleId))
        {
            throw new ArgumentException();
        }

        RobozzleEntities entities = new RobozzleEntities();
        Level level = entities.Level.Where(r => r.Id == puzzleId).First();
        if (level == null) throw new ArgumentException();

        return
            "[{\n"
            + "   id:" + level.Id + ",\n"
            + "   title:" + EncodeJsString(level.Title) + ",\n"
            + "   about:" + EncodeJsString(level.About) + ",\n"
            + "   robotCol:" + level.RobotCol + ",\n"
            + "   robotRow:" + level.RobotRow + ",\n"
            + "   robotDir:" + level.RobotDir + ",\n"
            + "   subs:[" + String.Join(",", RobozzleService.IntToSubs(level.Subs).Select(x=>x.ToString()).ToArray()) + "],\n"
            + "   allowedCommands:" + level.AllowedCommands + ",\n"
            + "   board:" + EncodeJsString(level.Board.Replace('#', ' ')) + "\n"
            + "}]";
    }

    /// <summary>
    /// Encodes a string to be represented as a string literal. The format
    /// is essentially a JSON string.
    /// 
    /// The string returned includes outer quotes 
    /// Example Output: "Hello \"Rick\"!\r\nRock on"
    /// 
    /// Source: http://www.west-wind.com/weblog/posts/114530.aspx
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string EncodeJsString(string s)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("\"");
        foreach (char c in s)
        {
            switch (c)
            {
                case '\"':
                    sb.Append("\\\"");
                    break;
                case '\\':
                    sb.Append("\\\\");
                    break;
                case '\b':
                    sb.Append("\\b");
                    break;
                case '\f':
                    sb.Append("\\f");
                    break;
                case '\n':
                    sb.Append("\\n");
                    break;
                case '\r':
                    sb.Append("\\r");
                    break;
                case '\t':
                    sb.Append("\\t");
                    break;
                default:
                    int i = (int)c;
                    if (i < 32 || i > 127)
                    {
                        sb.AppendFormat("\\u{0:X04}", i);
                    }
                    else
                    {
                        sb.Append(c);
                    }
                    break;
            }
        }
        sb.Append("\"");

        return sb.ToString();
    }
}
